package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.enums.DeleteFlag;
import com.example.mapper.SessionMessageMapper;
import com.example.pojo.SessionMessage;
import com.example.service.SessionMessageService;
import lombok.AllArgsConstructor;
import org.springframework.ai.chat.messages.Message;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@AllArgsConstructor
public class SessionMessageServiceImpl extends ServiceImpl<SessionMessageMapper, SessionMessage> implements SessionMessageService{
    private final SessionMessageMapper sessionMessageMapper;
    @Override
    public void addSession(String conversationId, List<Message> messages) {
        messages.forEach(message -> {
            SessionMessage sessionMessage = new SessionMessage();
            sessionMessage.setSessionId(conversationId);
            sessionMessage.setRole(message.getMessageType().getValue());
            sessionMessage.setContent(message.getText());
            sessionMessage.setMessageOrder(1L);
            sessionMessage.setDeleteFlag(DeleteFlag.NOT_DELETE.status);
            sessionMessage.setCreateTime(LocalDateTime.now());
            sessionMessageMapper.insert(sessionMessage);
        });
    }

    @Override
    public List<SessionMessage> getSessionMessageList(String sessionId) {
        LambdaQueryWrapper<SessionMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SessionMessage::getSessionId, sessionId)
                .orderByAsc(SessionMessage::getCreateTime);
        List<SessionMessage> list = sessionMessageMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public void deleteSessionMessage(String sessionId) {
        LambdaUpdateWrapper<SessionMessage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SessionMessage::getSessionId, sessionId)
                .set(SessionMessage::getDeleteFlag, DeleteFlag.DELETE.status);
        sessionMessageMapper.update(updateWrapper);
    }
}




